﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using KegCommunication.Communication;

namespace KegCommunication
{
    public class Receiver : IDisposable
    {
        public Receiver() : this(DefaultPipeName) { }
        public Receiver(string PipeName)
        {
            _PipeName = PipeName;
        }

        public bool ServiceOn()
        {
            return (_operational = HostThisService());
        }

        public void ServiceOff()
        {
            if (_host != null)
            {
                if (_host.State != CommunicationState.Closed)
                {
                    _host.Close();
                }
            }
            _operational = false;
        }

        private bool HostThisService()
        {
            try
            {
                _host = new ServiceHost(_ps, new Uri(PipeService.URI));
                // Usage BasicHttpBinding can be used if this is
                // not going to be on the local machine.
                // TODO: Convert to httpbinding
                _host.AddServiceEndpoint(typeof(IPipeService), new NetNamedPipeBinding(), _PipeName);
                _host.Open();
                _operational = true;
            }
            catch (Exception ex)
            {
                error = ex;
                _operational = false;
            }

            return _operational;
        }

        // The consumer of this class will subscribe to this delegate to
        // receive data in a callback fashion.
        public PipeService.DataIsReady Data
        {
            get
            {
                return _ps.DataReady;
            }
            set
            {
                _ps.DataReady += value;
            }
        }

        public PipeService.StartPour StartPour
        {
            get
            {
                return _ps.OnStartPour;
            }
            set
            {
                _ps.OnStartPour += value;
            }
        }

        public PipeService.ContinuePour ContinuePour
        {
            get
            {
                return _ps.OnContinuePour;
            }
            set
            {
                _ps.OnContinuePour += value;
            }
        }

        public PipeService.EndPour EndPour
        {
            get
            {
                return _ps.OnEndPour;
            }
            set
            {
                _ps.OnEndPour += value;
            }
        }

        public PipeService.UpdateTemperature UpdateTemperature
        {
            get
            {
                return _ps.OnUpdateTemperature;
            }
            set
            {
                _ps.OnUpdateTemperature += value;
            }
        }

        public PipeService.UserAuthenicated UserAuthenticated
        {
            get
            {
                return _ps.OnUserAuthenticated;
            }
            set
            {
                _ps.OnUserAuthenticated += value;
            }
        }

        public string CurrentPipeName
        {
            get { return _PipeName; }
        }

        // Any error text will be placed here.
        public Exception error = null;

        // See the actual name of the pipe for
        // any default operations.
        public const string DefaultPipeName = "Pipe1";

        private PipeService _ps = new PipeService();
        private bool _operational = false;
        private ServiceHost _host = null;
        private string _PipeName = string.Empty;

        // A basic dispose.
        public void Dispose()
        {
            this.ServiceOff();
            if (_ps != null)
            {
                _ps = null;
            }
        }
    }
}
